package com.hht;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;

/**
 * @Author huhaitao21
 * @Description //TODO
 * @Date 14:42 2022/7/8
 **/
public class ThreadExceptionDemo {
    static  Logger LOGGER = LoggerFactory.getLogger(ThreadExceptionDemo.class);
    public static void main(String[] args) {
//        case1();
        //单独捕获
        case2();
        //设置未捕获异常处理器
        case3();

        //继承ThreadPoolExecutor重写afterExecute方法
        //使用Thread.setUncaughtExceptionHandler方法设置异常处理器
        //使用ThreadGroup重写uncaughtException方法
    }

    static void case1(){
        Runnable runnable = () -> {
            System.out.println(5 / 0);
            System.out.println(11111);
        };
        try {
            new Thread(runnable).start();
        } catch (Exception e) {
            System.out.println("外部抓取异常");
            e.printStackTrace();
        }
    }

    static void case2(){
        Runnable runnable = () -> {
            try {
                System.out.println(5 / 0);
                System.out.println(11111);
            }catch (Exception e){
                System.out.println("内部抓取异常");
                e.printStackTrace();
            }

        };
        try {
            new Thread(runnable).start();
        } catch (Exception e) {
            System.out.println("外部抓取异常");
            e.printStackTrace();
        }
    }

    static void case3(){
        Runnable runnable1 = () -> {
            System.out.println(5 / 0);
            System.out.println(11111);
        };
        Runnable runnable2 = () -> {
            int[] ints = new int[1];
            System.out.println(ints[2]);
        };
        CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        thread1.setUncaughtExceptionHandler(customExceptionHandler);
        thread2.setUncaughtExceptionHandler(customExceptionHandler);
        thread1.start();
        thread2.start();
    }
    public static class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            System.out.println("线程执行异常");
            e.printStackTrace();
        }
    }


    static void case4(){
        Runnable runnable1 = () -> {
            System.out.println(5 / 0);
            System.out.println(11111);
        };
        ThreadGroup threadGroup = new ThreadGroup("threadGroup") {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println("全局线程异常处理：线程执行异常");
                e.printStackTrace();
            }
        };

        Thread thread1 = new Thread(threadGroup, runnable1);
        thread1.start();
    }


    static void  case5(){
        Runnable runnable1 = () -> {
            System.out.println(5 / 0);
            System.out.println(11111);
        };
        Runnable runnable2 = () -> {
            int[] ints = new int[1];
            System.out.println(ints[2]);
        };
        Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler2());
        CustomExceptionHandler1 customExceptionHandler = new CustomExceptionHandler1();
        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        thread1.setUncaughtExceptionHandler(customExceptionHandler);
        thread1.start();
        thread2.start();
    }


    public static class CustomExceptionHandler1 implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            LOGGER.error("线程执行异常", e);
        }
    }

    public static class CustomExceptionHandler2 implements Thread.UncaughtExceptionHandler {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            LOGGER.error("全局线程异常处理：线程执行异常", e);
        }
    }

}
